<?php
class SiteMap_Extractor extends Base_Extractor
{
  const DEEP_LEVEL = 3;
  
  public $count_page  = 0;
  /**
   * Store Processed pages
   * @var array
   */
  public $processed   = array();
  
  public function getSiteMap($url)
  {
    return array($url => $this->doParsePage($url, - 1));
  }
  /**
   * Method to get site structure with page options 
   * @param string $url
   */
  public function doParsePage($url, $level)
  {
    $level ++;
    $this->count_page ++;
    $this->processed[$url] = $level;
    
    $links = $this->getInnerLinks($url);
    
    foreach($links as $link)
    {
      if(! array_key_exists($link, $this->processed))
      {
        $this->processed[$link] = $level + 1;
      }
    }
    
    $child_links = array();
    
    if($level < self::DEEP_LEVEL)
    {
      foreach($links as $link)
      {
        if(isset($this->processed[$link]) && is_numeric($this->processed[$link]))
        {
          if($this->processed[$link] == $level + 1)
          {
            $child_links[$link] = $this->doParsePage($link, $level);
            $this->processed[$link] = null;
          }
        }
        continue;
      }
    }
    echo "Level: ", $level, " Link: ", $url, "\n";
    $inner_links = array_unique($this->getInnerLinks($url));
    $outer_links = array_unique($this->getOuterLinks($url));
    return array("level" => $level, "links_outer" => $outer_links, "links_inner" => $inner_links, "child" => $child_links);
  }
}
?> 